<template>
  <div>
    <el-row style="width: 92%;margin: 20px auto">
      <el-button @click="showAddpoint = true" plain>新增配送点</el-button>
    </el-row>
    <el-table :data="pointList" style="width: 92%;margin: 10px auto">
      <el-table-column prop="hash.belongschool" label="所属学校" sortable>
      </el-table-column>
      <el-table-column prop="hash.pickname" label="名称">
      </el-table-column>
      <el-table-column prop="hash.address" label="地址">
      </el-table-column>
      <el-table-column prop="hash.status" label="状态">
      </el-table-column>
      <el-table-column
        label="操作"
        width="100"
      >
        <template slot-scope="scope">
          <el-button type="text" size="primary" @click="changePoint(scope.row)">修改</el-button>
          <el-button type="text" size="primary" @click="deleteItem(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      title="新增配送点"
      :visible.sync="showAddpoint"
      width="40%"
    >
      <el-form labelWidth="100px">
        <el-form-item label="配送点名称">
          <el-input v-model="pointName"></el-input>
        </el-form-item>
        <el-form-item label="配送点地址">
          <el-input placeholder="请选择地址" v-model="localPoint.name" :readonly="true">
            <template slot="append"><i class="iconfont icon-shouhuodizhi" @click="isshowMap" style="font-size:25px"></i></template>
          </el-input>
        </el-form-item>
        <el-form-item label="选择学校">
          <el-checkbox-group
            v-model="checkedSchool"
          >
            <el-checkbox v-for="item in schoolList" :label="item.hash.schoolid" :key="item.hash.schoolid">{{item.hash.schoolname}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showAddpoint = false">取 消</el-button>
        <el-button type="primary" @click="newPoint">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="修改配送点"
      :visible.sync="revisePoint"
      width="40%"
    >
      <el-form labelWidth="100px">
        <el-form-item label="配送点名称">
          <el-input v-model="schoolList1.pickname"></el-input>
        </el-form-item>
        <el-form-item label="配送点地址">
          <el-input placeholder="请输入内容" v-model="localPoint.name">
            <template slot="append"><i class="iconfont icon-shouhuodizhi" @click="isshowMap" style="font-size:25px"></i></template>
          </el-input>
        </el-form-item>
        <el-form-item label="选择学校">
          <el-checkbox-group
            v-model="checkedSchool2"
          >
            <el-checkbox v-for="item in schoolList2" :label="item.hash.schoolid" :key="item.hash.schoolid">{{item.hash.schoolname}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="配送点状态">
          <el-radio v-model="pointStatus" label="1">启用</el-radio>
          <el-radio v-model="pointStatus" label="0">禁用</el-radio>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="revisePoint = false">取 消</el-button>
        <el-button type="primary" @click="savechangePoint">修 改 </el-button>
      </span>
    </el-dialog>
    <qqmap v-show="showMap" :isshowMap="this.isshowMap" v-if="isRouterAlive" :chooseLocal="chooseLocal"
      @getLocal="this.getLocal"
    ></qqmap>
  </div>
</template>

<script>
import qqmap from '~/components/base/qqmap.vue';

export default {
  data() {
    return {
      ts: this.newStore(),
      oldname: '',
      pid: '',
      pointStatus: '',
      chooseLocal: { lat: '39.06552', lng: '117.11415' },
      pointAdr: '',
      pointName: '',
      localPoint: {},
      isRouterAlive: true,
      showAddpoint: false,
      revisePoint: false,
      showMap: false,
      abelPosition: 'right',
      pointList: [],
      schoolList: [],
      schoolList1: [],
      schoolList2: [],
      checkedSchool: [],
      checkedSchool2: [],
    };
  },
  components: {
    qqmap,
  },
  created() {
    this.getPointList();
    this.getSchoolList();
  },
  methods: {
    savechangePoint() {
      const req = {
        handle: '47EEC3EB7CDCBB51AB6E160D70E90142883D0F8D3FCA25ADA76AB68AE763D87C',
        pickid: this.pid,
        pickname: this.schoolList1.pickname,
        lng: this.chooseLocal.lng,
        lat: this.chooseLocal.lat,
        schoolid: this.checkedSchool2,
        address: this.localPoint.name,
        status: this.pointStatus,
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this.$message({
          message: '修改成功！',
          type: 'success',
        });
        this.getPointList();
        this.revisePoint = false;
      });
    },
    changePoint(item) {
      this.pid = item.hash.pickid;
      this.checkedSchool2 = [];
      this.revisePoint = true;
      const req = {
        handle: '47EEC3EB7CDCBB51AB6E160D70E90142C2E4380F763630A4A76AB68AE763D87C',
        pickid: item.hash.pickid,
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        console.log(ts.d);

        this.schoolList2 = ts.d.data.operation2;
        for (let i = 0; i < this.schoolList2.length; i += 1) {
          if (this.schoolList2[i].hash.belongschool == 1) {
            this.checkedSchool2.push(this.schoolList2[i].hash.schoolid);
          }
        }
        this.schoolList1 = ts.d.data.operation1[0].hash;
        this.localPoint.name = this.schoolList1.address;
        this.chooseLocal.lat = this.schoolList1.lat;
        this.chooseLocal.lng = this.schoolList1.lng;
        this.pointStatus = this.schoolList1.status;
      });
    },
    newPoint() {
      this.reload();
      const req = {
        handle: '47EEC3EB7CDCBB51AB6E160D70E901424E95A8D2219E38CB3B1B9C55D174EDC8',
        pickname: `${this.pointName}配送点`,
        address: this.localPoint.name,
        lng: this.localPoint.latLng.lng,
        lat: this.localPoint.latLng.lat,
        schoolid: this.checkedSchool,
      };
      if (this.localPoint.name && this.pointName && this.checkedSchool) {
        this.spost(this.ts, '/mainservlet', req).then((ts) => {
          this.showAddpoint = false;
          this.$message({
            message: '添加成功！',
            type: 'success',
          });
          this.getPointList();
        });
      } else {
        this.$message({
          type: 'info',
          message: '不能为空',
        });
      }
    },
    getSchoolList() {
      const req = {
        handle: '47EEC3EB7CDCBB51AB6E160D70E90142578A3E9390AE65D16DB7732D4F0BAC3D627154A1FBB7F4B9',
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this.schoolList = ts.d.data.operation1;
      });
    },
    getPointList() {
      const req = {
        handle: '1E8254760FECAC00839BD73D1C4F9A6559B7AE69B5ABEA2DE4A15B9858851EA9',
        schoolid: '',
        pickname: '',
        status: '',
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this.pointList = ts.d.data.operation1;
        for (let i = 0; i < this.pointList.length; i += 1) {
          if (this.pointList[i].hash.status == 1) {
            this.pointList[i].hash.status = '启用';
          } else {
            this.pointList[i].hash.status = '禁用';
          }
        }
        console.log(this.pointList);
      });
    },
    getLocal(data) {
      this.localPoint = data;
      this.chooseLocal = data.latLng;
      console.log(this.chooseLocal);
    },
    isshowMap() {
      this.reload();
      this.showMap = !this.showMap;
    },
    reload() {
      this.isRouterAlive = false;
      this.$nextTick(() => {
        this.isRouterAlive = true;
      });
    },
    deleteItem(item) {
      const req = {
        handle: '47EEC3EB7CDCBB51AB6E160D70E90142F568A6CE28C87E07199CCFDC9571138D2D0E581B2200E4FD',
        pickid: item.hash.pickid,
      };
      this.$confirm('是否删除该条记录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.spost(this.ts, '/mainservlet', req).then((ts) => {
          this.$message({
            type: 'success',
            message: '删除成功!',
          });
          this.getPointList();
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除',
        });
      });
    },
  },
};
</script>

<style lang="stylus" scoped>
.num
  display flex
  justify-content space-around
  span
    font-size 24px
.el-col
  padding: 20px 10px;
.c_top
  div
    background #fff
    margin 20px 0
    p
      height 30px
      line-height 30px
      margin-bottom 10px
      &:nth-child(1)
        color #aaa
        span
          &:first-child
            float left
          &:last-child
            float right
            b
              color #409EFF
      &:nth-child(2)
        font-size 30px
        padding-left 25px
      &:last-child
        padding-top 10px
        border-top 1px solid #ccc
        color #aaa
        b
          color black
          float right
</style>
